package org.springboot.szsrcb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.szsrcb.entity.Req_bill;
import org.springboot.szsrcb.entity.User;
import org.springboot.szsrcb.srvice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class loginRestController {

	private static final Logger logger = LoggerFactory.getLogger(loginRestController.class);

	@Autowired
	private UserService userService;
	
	@RequestMapping("/change_pwd_save")
	public String change_pwd_save(HttpServletRequest request,HttpSession session) {
		Map<String,Object> map=new HashMap<String,Object>();
		String current_login_name=(String) session.getAttribute("login_name");
		String get_old_pwd=(String) request.getParameter("old_pwd");
		String get_new_pwd=(String) request.getParameter("new_pwd1");
		map.put("login_name", current_login_name);
		map.put("old_pwd", get_old_pwd);
		map.put("new_pwd", get_new_pwd);
		boolean check_r=userService.checkUserPWD(current_login_name,get_old_pwd);
		if (check_r) {
			userService.change_pwd(current_login_name,get_new_pwd);
			return "true";		
		} else {		
			return "录入的旧密码不正确，请检查录入";		
		}
		
	}
	@RequestMapping("/login")
	public String login(HttpServletRequest request, Model model, User user,HttpSession session) {
		String result;
		result = "";
		logger.info("rest controller login");
		model.addAttribute("user", user);
		
		logger.info("login user:" + user.getLogin_name());
		//logger.info("pwd user:" + user.getPwd());
		//logger.info("request.getMethod:" + request.getMethod());
		//Object t=session.getAttribute("login_name");
		
		
		if (user.getLogin_name() == null) {
			result = result + " 用户名为空";
		}
		if (user.getPwd() == null) {
			result = result + " 密码为空";
		}
		if (result.length() < 3) {
			boolean c_name = userService.checkLoginName(user.getLogin_name());
			if (c_name) {
				boolean get_r = userService.checkUserPWD(user.getLogin_name(), user.getPwd());
				if (get_r) {
					result = "true";
					User u =userService.search_a_user(user.getLogin_name());
					session.setAttribute("user", u.getName());  
					session.setAttribute("login_name", user.getLogin_name());  
				} else {
					result = result + "密码不正确，请检查录入数据！";
				}

			} else {
				result = result + "用户不存在，请检查录入数据！";
			}
		}

		return result;
	}
	
	@RequestMapping("/regrister_form_load")
	public String regrister_form_load(HttpServletRequest request, Model model, User user,HttpSession session) {
		String result;
		result = "";
		result=trans_2_json_regrister_form_load();

		return result;
	}
	
	private String trans_2_json_regrister_form_load() {
		
		String r_start="["
						+ "{type: 'fieldset', offsetLeft:'150',label:'注册用户', list:["
										+"{type:'block',list:["						
											+"{type : 'input',name : 'name',label : '姓名',labelWidth:'80',inputWidth:'150',position:'label-left', required:'true', offsetLeft:'25'},"											
											+"{type : 'input',name : 'login_name',label : '登录名',labelWidth:'80',inputWidth:'150',position:'label-left', required:'true', offsetLeft:'25'},"
											+"{type : 'input',name : 'pwd',label : '密码',labelWidth:'80',inputWidth:'150',position:'label-left', required:'true', offsetLeft:'25'},"
											+"{type : 'select',name : 'role',label : '角色/权限',labelWidth:'80',inputWidth:'150', offsetLeft:'25',required:'true',options:["
												//	+"{text: '', value: ''},"
													+"{text: '普通用户', value: '普通用户'},"
													+"{text: '需求录入用户', value: '需求录入用户'},"
													+"{text: '开发中心测试申请录入', value: '开发中心测试申请录入'}"
												//	+"{text: '项目管理中心负责人', value: '项目管理中心负责人'},"
												//	+"{text: '业务方案中心负责人', value: '业务方案中心负责人'},"
												//	+"{text: '部门主管领导',value: '部门主管领导'},"
												//	+"{text: '系统管理员',value: 'admin'}"
											+ "]}"
										+"]},"
										+"{type:'block',list:["
											+"{type : 'button',name : 'save_btn',value : '保存信息'},"																				
										+"]}"										
																			
									+"]"
							+ "}"
						+"]";
		String r_s=r_start;
		logger.info("retrun info :" + r_start);
		return r_s;
	}
	
	@RequestMapping("/regrister_save")
	public String regrister_save(HttpServletRequest request, Model model, User user,HttpSession session) {
				
		logger.info("regrister_save ");
		model.addAttribute("user", user);
		
		String g_UserName=user.getName();
		boolean r_checkUserName=userService.checkUserName(g_UserName);
		if(r_checkUserName){
			return "用户名["+g_UserName+"]已存，还请检查录入";
		}
		else{
			String g_Login_name=user.getLogin_name();
			boolean r_Login_name=userService.checkLoginName(g_Login_name);
			if (r_Login_name) {
				return "登录名["+g_Login_name+"]已存，还请检查录入";
			} else {
				boolean r = userService.regrister_user(user);
				if (r) {
					return "true";
				} else {
					return "新建用户失败，请与管理员联系";
				}
				
			}
		}
	
		
	
		
		
	}	
}
